package com.zzj.压缩7z;

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 小郑
 * Date: 2017-08-04
 * Time: 14:26
 */
public class Unarctive {
    /**
     * @param orgPath 源压缩文件地址
     * @param tarpath 解压后存放的目录地址
     * @author kxl
     */
    public static List<File> apache7ZDecomp(String orgPath, String tarpath) {
        List<File> files = new ArrayList<>();
        try {
            SevenZFile sevenZFile = new SevenZFile(new File(orgPath));
            SevenZArchiveEntry entry = sevenZFile.getNextEntry();
            while (entry != null) {
                if (entry.isDirectory()) {
                    new File(tarpath + entry.getName()).mkdirs();
                    entry = sevenZFile.getNextEntry();
                    continue;
                }
                FileOutputStream out = new FileOutputStream(tarpath
                        + File.separator + entry.getName());
                files.add(new File(tarpath
                        + File.separator + entry.getName()));
                byte[] content = new byte[(int) entry.getSize()];
                sevenZFile.read(content, 0, content.length);
                out.write(content);
                out.close();
                entry = sevenZFile.getNextEntry();
            }
            sevenZFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return files;
    }

    public static void main(String[] args) {
        File file = new File("d:\\bla222");
        if (!file.exists()) {
            file.mkdirs();
        }
        List<File> flist = apache7ZDecomp("d:\\bla.7z", "d:\\bla222");
        for(File f:flist){
            System.out.println(f.getName());
        }
    }
}
